<?php
/*
	提现管理
*/
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();

if($_RQ['op']=='list'){
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']=array();
	//会员ID
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(isset($_RQ['type'])){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	//时间范围
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getslice('core_member_cash',$params,array($pindex,$psize),$total,array(),'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=model('core/core')->member_show($item['mid']);
			$fans=pdo_get('core_fans',array('mid'=>$item['mid']),array('wxgid'));
			if($item['member']['from_os']==1){
				$item['apptitle']=pdo_getcolumn('core_account_wxapp',array('id'=>$fans['wxgid']),'title');
				$item['apptype']='小程序';
			}else{
				$item['apptitle']=pdo_getcolumn('core_account_wechat',array('id'=>$fans['wxgid']),'title');
				$item['apptype']='公众号';
			}
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'会员提现申请'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：</br>【<red>提示:NO_AUTH产品权限验证失败？</red>】</br>1、站点设置--支付参数--微信支付那边要上传微信支付证书；2、微信商户那边要开启“企业付款到零钱”的权限；','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员','type'=>'member'),
			array('name'=>'apptype','title'=>'来源','type'=>'string'),
			array('name'=>'apptitle','title'=>'所属','type'=>'string'),
			array('name'=>'type','title'=>'提现类型','type'=>'radio','options'=>'type_array'),
			array('name'=>'ordersn','title'=>'提现单号','type'=>'string'),
			array('name'=>'money','title'=>'提现金额','type'=>'string'),
			array('name'=>'service_money','title'=>'提现手续费','type'=>'string'),
			array('name'=>'cash_money','title'=>'预计到账金额','type'=>'string'),
			array('name'=>'status','title'=>'提现状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'log','title'=>'现金日志'),
			array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>0)),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'title'=>'提现状态','name'=>'status','options'=>'statuss'),
			array('type'=>'radio','width'=>15,'title'=>'提现类型','name'=>'type','options'=>'type_array'),
			array('type'=>'choose','width'=>15,'title'=>'会员id','name'=>'mid','action'=>'member.get_member','choose_title'=>'选择会员'),
			array('type'=>'time_range','width'=>18,'title'=>'创建时间','name'=>'createtime','help'=>''),
		),
	);
	$return['statuss']=array(
		'0'=>'待审核',
		'1'=>'已打款',
		'2'=>'审核不通过',
	);
	$return['type_array']=array('提现到微信钱包','提现到支付宝','提现到银行卡');
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$item=pdo_get('core_member_cash',array('id'=>$id));
		$type=intval($post['type']);
		$status=intval($post['status']);
		if(!$status){
			exi('请先选择是否审核通过！','error');
		}
		if($status==1){
			if($type==0){
				model('core/member/cash')->pay($id);
			}else{
				pdo_update('core_member_cash',array('status'=>1),array('id'=>$id));
			}
			exi('打款成功！','','list');
		}else{
			if(!$post['remark']){
				exi('请填写审核不通过原因！','error');
			}
			pdo_update('core_member_cash',array('status'=>2,'remark'=>trim($post['remark'])),array('id'=>$id));
			$res=model('core/member')->thaw_credit2($item['mid'],$item['money'],'会员提现解冻','core');
			if($res){
				model('core/member')->add_credit2($item['mid'],$item['money'],'会员提现失败','core');
			}
			exi('操作成功！','','list');
		}
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('core_member_cash',array('id'=>$id));
	if(!$item){
		exi('指定提现申请不存在~','error');
	}
	if($item['status']){
		exi('该提现申请已审核，请勿重复审核~','error');
	}
	
	$return['statuss']=array(
		'1'=>'审核通过并打款',
		'2'=>'审核不通过',
	);
	$return['type_array']=array('提现到微信钱包','提现到支付宝','提现到银行卡');
	$item['typetitle']=$return['type_array'][$item['type']];
	$member=model('core/core')->member_show($item['mid']);
	$item['wxgid']=pdo_getcolumn('core_fans',array('mid'=>$item['mid']),'wxgid');
	if($member['from_os']==1){
		$item['apptitle']=pdo_getcolumn('core_account_wxapp',array('id'=>$item['wxgid']),'title');
	}else{
		$item['apptitle']=pdo_getcolumn('core_account_wechat',array('id'=>$item['wxgid']),'title');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'tip','title'=>'【温馨提示】：如果打款会员是<red>小程序</red>会员，会显示所属《小程序》，否则可能会打款不成功!默认打款使用的是公众号！'),
			array('name'=>'apptitle','title'=>'所属','type'=>'copy'),
			array('type'=>'copy','name'=>'typetitle','title'=>'提现类型'),
			array('type'=>'copy','name'=>'money','title'=>'提现金额'),
			array('type'=>'copy','name'=>'cash_money','title'=>'真实到账金额'),
			array('type'=>'copy','name'=>'realname','title'=>'姓名','show'=>array('type'=>array(1,2))),
			array('type'=>'copy','name'=>'alipay','title'=>'支付宝账号','show'=>array('type'=>array(1))),
			array('type'=>'copy','name'=>'bankname','title'=>'所属银行','show'=>array('type'=>array(2))),
			array('type'=>'copy','name'=>'bankcard','title'=>'银行卡号','show'=>array('type'=>array(2))),
			array('type'=>'radio','name'=>'status','title'=>'审核','options'=>'statuss'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'string','show'=>array('status'=>2)),
		),
	);
	$return['wxapps']=pdo_getall('core_account_wxapp',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	exi($return);
}
if($_RQ['op']=='log'){
	$return['types']=array(
		'1'=>'积分',
		'2'=>'余额'
	);
	$return['adds']=array(
		'0'=>'消费',
		'1'=>'获得',
		'2'=>'提现',
	);
	$return['types2']=array(
		array('id'=>1,'title'=>'积分'),
		array('id'=>2,'title'=>'余额'),
	);
	$return['adds2']=array(
		array('id'=>0,'title'=>'消费'),
		array('id'=>1,'title'=>'获得'),
		array('id'=>2,'title'=>'提现'),
	);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']=array();
	//exi('!!'.$_RQ['id'],'error');
	$params['mid']=pdo_getcolumn('core_member_cash',array('id'=>$_RQ['id']),'mid');
	$return['params']['mid']=$params['mid'];
	$params['type']=2;
	$return['params']['type']=2;
	//时间范围
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getslice('core_credit_log',$params,array($pindex,$psize),$total,array(),'','id desc');
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'mid','title'=>'会员id','type'=>'string'),
			array('name'=>'type','title'=>'积分类型','type'=>'radio','options'=>'types'),
			array('name'=>'is_add','title'=>'日志类型','type'=>'radio','options'=>'adds'),
			array('name'=>'fee','title'=>'金额','type'=>'string'),
			array('name'=>'plugin','title'=>'插件','type'=>'string'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
	);
	exi($return);
}